package gameClient.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;

/**
 * 完成登录页面制作，带背景图美化版本
 */
public class Login extends JFrame {
    private JLabel usernameJLabel;       // 用户名标签
    private JTextField usernameTextField;// 用户名输入框
    private JButton loginJButton;        // 登录按钮
    private JButton cancelJButton;       // 取消按钮

    public Login() {
        this.setTitle("登录");
        this.setSize(500, 400);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);

        // 1. 加载背景图
        String projectRoot = System.getProperty("user.dir");
        String imagePath = projectRoot + "/client/images/client-login-background.jpg";
        Image backgroundImage = new ImageIcon(imagePath).getImage();

        // 2. 创建层叠面板，用于叠加背景和控件
        JLayeredPane layeredPane = new JLayeredPane();
        layeredPane.setPreferredSize(new Dimension(500, 400));

        // 3. 背景图片标签
        JLabel backgroundLabel = new JLabel(new ImageIcon(backgroundImage));
        backgroundLabel.setBounds(0, 0, 500, 400);

        // 4. 透明面板放控制件，使用GridBagLayout布局
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setOpaque(false);
        panel.setBounds(70, 100, 360, 150);


        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 5. 初始化组件
        usernameJLabel = new JLabel("用户名:");
        usernameJLabel.setFont(new Font("微软雅黑", Font.BOLD, 22));
        usernameJLabel.setForeground(Color.WHITE);

        usernameTextField = new JTextField(20);
        // 调整文本框高度
        usernameTextField.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        usernameTextField.setPreferredSize(new Dimension(200, 40));

        loginJButton = new JButton("登录");
        cancelJButton = new JButton("取消");

        // 按钮字体变大，尺寸变大
        loginJButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        loginJButton.setPreferredSize(new Dimension(110, 45));
        cancelJButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        cancelJButton.setPreferredSize(new Dimension(110, 45));

        // 简单美化按钮
        loginJButton.setFocusPainted(false);
        cancelJButton.setFocusPainted(false);

        // 6. 添加组件到panel
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 0.3;
        panel.add(usernameJLabel, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 0.7;
        panel.add(usernameTextField, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.weightx = 0.5;
        panel.add(loginJButton, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        panel.add(cancelJButton, gbc);

        // 7. 绑定事件监听器
        Myevent event = new Myevent();
        loginJButton.addActionListener(event);
        cancelJButton.addActionListener(e -> {
            // 关闭程序
            System.exit(0);
        });


        // 8. 把背景图放底层，控件放上层
        layeredPane.add(backgroundLabel, Integer.valueOf(0));
        layeredPane.add(panel, Integer.valueOf(1));

        // 9. 设置内容面板为layeredPane
        this.setContentPane(layeredPane);
        this.pack();
        this.setVisible(true);
    }

    // 内部监听类
    class Myevent implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = usernameTextField.getText();
            System.out.println("name:" + username);

            try {
                Socket socket = new Socket("localhost", 8888);
                System.out.println("客户端" + socket);
                // 跳转到主窗体
                new ClientMainFrame(username,socket);
            } catch (IOException ex) {
                ex.printStackTrace();
            }


        }
    }
}

